<?php
//
namespace app\circle\controller\admin;

use app\circle\model\AdminModel;
use app\circle\model\CircleMemberModel;
use app\circle\model\CircleModel;
use app\circle\model\ClassModel;
use app\circle\model\ThemeModel;
use think\Db;
use think\Exception;
use think\Request;

class CircleController extends BaseController
{

    private $adminModel;
    private $circleModel;
    private $classModel;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->circleModel = new CircleModel();
        $this->classModel = new ClassModel();
        $this->adminModel = new AdminModel();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $map        = [];
        $query      = $this->request->request();
        $circleName  = $this->request->param('circle_name');
        $map['is_del'] = 0;
        if($this->adminInfo['admin_power']==3){
            $map['admin_id'] = $this->adminInfo['admin_id'];
        }
        if($this->adminInfo['admin_power']==2){
            $map['dept_id'] = $this->adminInfo['dept_id'];
        }
        if (!empty($circleName)) {
            $map['circle_name'] = ['like', '%' . $circleName . '%'];
        }
        $list = $this->circleModel->where($map)->paginate(12, false, ['query' => $query]);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加
     * @return mixed
     */
    public function add(){
        if($this->request->isAjax()){
            $data   = $this->request->param();
            $result = $this->validate($data, [
                'circle_name|圈子名称'  => 'require',
                'circle_class_id|圈子分类' => 'require|>:0',
                'circle_tag|圈子TAG' => 'require',
                'circle_mastername|圈子管理员' => 'require',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            $adminInfo = $this->adminModel->where('admin_name',$data['circle_mastername'])->find();
            if(!$adminInfo){
                $this->error('未找到该管理员！');
            }
            $data['circle_masterid'] = $adminInfo['admin_id'];
            $info = $this->classModel->where('class_id',$data['circle_class_id'])->find();
            if(!$info){
                $this->error('未找到对应圈子分类！');
            }
            $data['circle_class_name'] = $info['class_name'];
            $data['dept_id'] = $this->adminInfo['dept_id'];
            $data['dept_name'] = $this->adminInfo['dept_name'];
            $data['dept_path'] = $this->adminInfo['dept_path'];
            $data['admin_id'] = $this->adminInfo['admin_id'];
            $this->circleModel->addData($data);
            $this->success('操作成功！','index');
        }
        $circleClass = $this->classModel->order('class_sort')->select();
        $this->assign('circleClass',$circleClass);
        return $this->fetch();
    }

    /**
     * 编辑
     */
    public function edit(){
        if($this->request->isAjax()){
            $data   = $this->request->param();
            $result = $this->validate($data, [
                'circle_name|圈子名称'  => 'require',
                'circle_class_id|圈子分类' => 'require|>:0',
                'circle_tag|圈子TAG' => 'require',
                'circle_mastername|圈子管理员' => 'require',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            $adminInfo = $this->adminModel->where('admin_name',$data['circle_mastername'])->find();
            if(!$adminInfo){
                $this->error('未找到该管理员！');
            }
            $data['circle_masterid'] = $adminInfo['admin_id'];
            $info = $this->classModel->where('class_id',$data['circle_class_id'])->find();
            if(!$info){
                $this->error('未找到对应圈子分类！');
            }
            $data['circle_class_name'] = $info['class_name'];
            $this->circleModel->editData($data);
            $this->success('操作成功！','index');
        }
        $circleMemberModel=new CircleMemberModel();
        $circleId = $this->request->param('circle_id/d');
        $memberName=$circleMemberModel->where(['circle_id'=>$circleId,'is_identity'=>1])->value('member_name');
        $this->assign('member_name',$memberName);
        $info = $this->circleModel->where('circle_id',$circleId)->find();
        $circleClass = $this->classModel->order('class_sort')->select();
        $this->assign('circleClass',$circleClass);
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function setIdentity()
    {
        $model=new CircleMemberModel();
        $circleId=$this->request->param('circle_id',0);
        if ($this->request->has('member_id')){
            $memberId=$this->request->param('member_id',0);
            Db::startTrans();
            try{
                $model->where('circle_id',$circleId)->setField('is_identity',2);
                $model->where(['member_id'=>$memberId,'circle_id'=>$circleId])->setField('is_identity',1);
                $themeModel=new ThemeModel();
                $themeModel->where(['circle_id'=>$circleId])->setField('is_top',0);
                $themeModel->where(['member_id'=>$memberId,'circle_id'=>$circleId])->setField('is_top',1);
                Db::commit();
            }catch (Exception $e){
                Db::rollback();
                $this->error('指定失败，请稍后再试');
            }
            $this->success('指定成功');
        }else{
            $memberName=$this->request->param('member_name','');
            $memberInfo=$model->where(['member_name'=>['like',"%$memberName%"],'circle_id'=>$circleId])->select();
            return $memberInfo;
        }
    }
    /**
     * 删除
     */
    public function del(){
        $circleId = $this->request->param('circle_id/d');
        $this->circleModel->where('circle_id',$circleId)->setField('is_del',1);
        $this->success('删除成功！');
    }

}
